﻿/*
// +----------------------------------------------------------------------------+
// |                                                                            |
// |  Copyright (1999-2010) by Actify, Inc.                                     |
// |                                                                            |
// |  This program is proprietary with Actify, Inc., and is not to              |
// |  be reproduced or transmitted in any form or by any means or used in any   |
// |  manner except as authorized in writing by Actify, Inc.                    |
// |                                                                            |
// +----------------------------------------------------------------------------+
// |                                                                            |
// |   File Name : ResourceHelper.cs                                            |
// |  Written By : Clay Tornquist                                               |
// |        Date : Feb 09 2010                                                  |
// | Description :                                                              |
// |                                                                            |
// +----------------------------------------------------------------------------+
// |                                                                            |
// | Notes:                                                                     |
// |                                                                            |
// +----------------------------------------------------------------------------+

	Changes:
		[2010-02-09 CMT] - Initial implementation.

*/
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Resources;
using System.IO;

namespace SpinFireSL
{
	/// <summary>
	/// Help class to review resources from executable
	/// </summary>
	internal class ResourceHelper
	{
		#region Attributes

		static string _executingAssemblyName = null;

		#endregion

		#region Constructor

		internal ResourceHelper()
		{
		}

		#endregion

		#region Internal Members

		/// <summary>
		/// Retrieve the executing assembly name
		/// </summary>
		internal string ExecutingAssemblyName
		{
			get
			{
				if (_executingAssemblyName == null)
				{
					string name = System.Reflection.Assembly.GetExecutingAssembly().FullName;
					_executingAssemblyName = name.Substring(0, name.IndexOf(','));
				}
				return _executingAssemblyName;
			}
		}

		/// <summary>
		/// Get a bitmap from the executing assembly with a relative resource identifier
		/// </summary>
		/// <param name="relativeUri">the relative resource identifier</param>
		/// <returns>a bitmap or null if failure</returns>
		internal BitmapImage GetBitmap(string relativeUri)
		{
			return GetBitmap(relativeUri, ExecutingAssemblyName);
		}

		#endregion

		#region Private Members

		/// <summary>
		/// Get stream from a specific assembly name with a relative resource identifier
		/// </summary>
		/// <param name="relativeUri">the relative resource identifier</param>
		/// <param name="assemblyName">the assembly name to obtain the resource stream</param>
		/// <returns>a stream</returns>
		private Stream GetStream(string relativeUri, string assemblyName)
		{
			StreamResourceInfo res = Application.GetResourceStream(new Uri(assemblyName + ";component/" + relativeUri, UriKind.Relative));
			if (res == null)
			{
				res = Application.GetResourceStream(new Uri(relativeUri, UriKind.Relative));
			}
			if (res != null)
			{
				return res.Stream;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// Get a bitmap from a specific assembly name with a relative resource identifier
		/// </summary>
		/// <param name="relativeUri">the relative resource identifier</param>
		/// <param name="assemblyName">the assembly name to obtain the bitmap</param>
		/// <returns>a bitmap or null if failure</returns>
		private BitmapImage GetBitmap(string relativeUri, string assemblyName)
		{
			Stream s = GetStream(relativeUri, assemblyName);
			if (s == null) return null;
			using (s)
			{
				BitmapImage bmp = new BitmapImage();
				bmp.SetSource(s);
				return bmp;
			}
		}

		#endregion
	}
}
